package cn.zyk.exception;

import cn.zyk.result.CodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @Author eHlw
 * @Date 2024/9/13 20:49
 * @description:
 */

@Data
@EqualsAndHashCode(callSuper = false)
public class BizException extends RuntimeException{
    private int code = 500;

    private String msg;

    public BizException() {
        super(CodeEnum.UNKNOWN.getMsg());
        this.msg = CodeEnum.UNKNOWN.getMsg();
    }

    public BizException(CodeEnum codeEnum) {
        this.msg = codeEnum.getMsg();
        this.code = codeEnum.getCode();
    }

    public BizException(CodeEnum codeEnum, Throwable e) {
        super(codeEnum.getMsg(), e);
        this.msg = codeEnum.getMsg();
        this.code = codeEnum.getCode();
    }

    public BizException(String exception) {
        this.msg = exception;
    }

    /**
     * 通过以下两个方法自定义异常打印信息
     * @see #fillInStackTrace() 定义堆栈打印信息，默认打印全部调用链
     * @see #toString() 定义异常输出格式
     */
    @Override
    public synchronized Throwable fillInStackTrace() {
        if (this.code == 505)
            return this;
        return super.fillInStackTrace();
    }

    @Override
    public String toString() {
        return "错误信息为 >> {" +
                "msg='" + msg + '\'' +
                ", code=" + code +
                '}';
    }
}
